home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / ntblockecho.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-10-27  |  3.0 KB  |  130 lines

  1. /*
  2.     Name:        ntblockecho.rexx
  3.  
  4.     Version:     2.0
  5.  
  6.     Author:         alfie
  7.  
  8.     Date:        28.04.98
  9.  
  10.     Description: shows how to make a non blocking TCP connection;
  11.                  the stopping-connetction event is a msg on
  12.                  port "PORT";
  13.  
  14.     Usage:          rx ntblockecho <host>
  15.  
  16.     Note:         it is a simple echo client in itself.
  17.                  Be sure to have echo/tcp enabled in the service
  18.                  and inetd database, before testing it on localhost.
  19.                  Anyway, 'cause of a sort of delay is required to
  20.                  test the stopping-event, try this macro on a very
  21.                  slow non-local host with echo/tcp service.
  22. */
  23.  
  24. if ~show("L","rexxsupport.library") then
  25.     if ~addlib("rexxsupport.library",0,-30) then do
  26.         say "no rexxsupport.library"
  27.         exit
  28.     end
  29. if ~show("L","rxsocket.library") then
  30.     if ~addlib("rxsocket.library",0,-30) then do
  31.         say "no rxsocket.library"
  32.         exit
  33.     end
  34. if ~show("L","rmh.library") then
  35.     if ~addlib("rmh.library",0,-30) then do
  36.         say "no rmh.library"
  37.         exit
  38.     end
  39.  
  40. prg = ProgramName("NOEXT")
  41.  
  42. if ~RMH_ReadArgs("HOST/A") then do
  43.     call PrintFault(IoErr(),prg)
  44.     exit
  45. end
  46.  
  47. addr = resolve(parm.0.value)
  48. if addr=="-1" then call err "no host <"parm.0.value">"
  49.  
  50. /*set the remote addr*/
  51. sin.addrFamily = "INET"
  52. sin.addrAddr   = addr
  53.  
  54. /*try to find the echo service via GetServByName()*/
  55. if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
  56. else sin.addrPort = 7
  57.  
  58. /*open a port, a message on which will stop all*/
  59. if ~OpenPort("PORT") then call err "can't open my port"
  60.  
  61. /*get the port signal mask (not bit)*/
  62. portSig = PortSignal("PORT")
  63.  
  64. /*allocate a signal we want to be notified on for a socket events*/
  65. sigBit=AllocSignal()
  66. if sigBit==-1 then call err "can't allocate signal bit"
  67. sig=2**sigBit
  68.  
  69. /*create the socket*/
  70. sock = socket("INET","STREAM","IP")
  71. if sock<0 then call err "can't create socket ("errno()")"
  72.  
  73. /*the socket must NOT block*/
  74. call IOCtlSocket(sock,"FIONBIO",1)
  75.  
  76. /*notif us on connection*/
  77. call SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")
  78.  
  79. /*notify us via that signal we allocated*/
  80. SET.SIGEVENTMASK = sig
  81. call SetSocketBase("SET")
  82.  
  83. /*connect the echo service on the host*/
  84. res = connect(sock,"SIN")
  85. err=errno()
  86. if (res<0) & (err~=35) & (err~=36) then call err "connect error ("errno()")"
  87.  
  88. /*we even set a timeout of 10 seconds*/
  89. timeout = 10
  90. signal = or(portSig,sig)
  91. SEL.WRITE.0 = sock
  92. con = 0
  93. time = time("R")
  94. do while ~con & (time("E")<timeout)
  95.  
  96.     res = WaitSelect("SEL",timeout,0,signal)
  97.  
  98.     if and(SEL.SIGNALS,portSig)~=0 then do
  99.         pkt = GetPkt("PORT")
  100.         if pkt~=null() then do
  101.             m = GetArg(pkt)
  102.             call reply(pkt)
  103.             say "message on PORT:" m
  104.         end
  105.     end
  106.  
  107.     if res==1 then con = 1;
  108.  
  109. end
  110.  
  111. if ~con then call err "timeout"
  112.  
  113. say "--- connected ---"
  114.  
  115. call IOCtlSocket(sock,"FIONBIO",0)
  116.  
  117. request = "echo service test"
  118. res = send(sock,request)
  119. if res~=length(request) then call err "send errore ("errno()")"
  120.  
  121. len = recv(sock,"BUFF",256)
  122. if len<0 then call err "recv error ("errno()")"
  123. say buff
  124. exit
  125.  
  126. err: procedure expose prg
  127. parse arg msg
  128.     say prg":" msg
  129.     exit
  130.